home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / dev / misc / gms_dev.lha / GMS / Source / C / 3DObjects / Enneper.c next >
Encoding:
C/C++ Source or Header  |  1997-07-09  |  4.1 KB  |  165 lines

  1. /*
  2. ** Name:      Enepper's Surface.
  3. ** Version:   1.0
  4. ** Author:    Paul Manias
  5. ** Copyright: DreamWorld Productions (c) 1997
  6. ** SAS/C:     sc Enneper.c
  7. **
  8. ** Doc:       This is a demo of a rotating Enneper's Surface.
  9. **            The object is pre-calculated then rotated in real time for speed,
  10. **            although things could be faster than this.
  11. **
  12. ** Improve:   Use a pixel list rather than calling DrawPixel().
  13. **            Replace doubles with longs/words if possible.
  14. */
  15.  
  16. #include <proto/games.h>
  17. #include <math.h>
  18.  
  19. extern struct GMSBase *GMSBase;
  20. APTR   PREFSNAME = DEFAULT;
  21.  
  22. struct GameScreen *screen;
  23.  
  24. void Demo(void);
  25.  
  26. struct DotPixel { double X,Y,Z; };
  27.  
  28. #define AMTCOLOURS 32
  29.  
  30. ULONG palette[AMTCOLOURS] = {
  31.   0x000000,0x101010,0x171717,0x202020,0x272727,0x303030,0x373737,0x404040,
  32.   0x474747,0x505050,0x575757,0x606060,0x676767,0x707070,0x777777,0x808080,
  33.   0x878787,0x909090,0x979797,0xa0a0a0,0xa7a7a7,0xb0b0b0,0xb7b7b7,0xc0c0c0,
  34.   0xc7c7c7,0xd0d0d0,0xd7d7d7,0xe0e0e0,0xe0e0e0,0xf0f0f0,0xf7f7f7,0xffffff
  35. };
  36.  
  37. /***********************************************************************************/
  38.  
  39. void main(void)
  40. {
  41.   if (screen = AddScreenTags(TAGS_GAMESCREEN,NULL,
  42.      GSA_AmtColours, AMTCOLOURS,
  43.      GSA_Palette,    palette,
  44.      GSA_Attrib,     DBLBUFFER,
  45.      TAGEND)) {
  46.  
  47.      ShowScreen(screen);
  48.  
  49.      Demo();
  50.  
  51.   DeleteScreen(screen);
  52.   }
  53. }
  54.  
  55. /************************************************************************************
  56. ** Longtitude deterimines the position of the dot on the horizontal axis.
  57. ** Latitude determines the position of the dot on the vertical axis.
  58. */
  59.  
  60. #define AMTDOTS 1000       /* The amount of dots in the Object. */
  61.  
  62. #define MAXZ 1.96          /* MAXZ defined by 1.4*1.4 = 1.96 */
  63.  
  64. void Demo(void)
  65. {
  66.   struct DotPixel *object;
  67.   WORD   i;
  68.   WORD   offsetx = (screen->ScrWidth/2);
  69.   WORD   offsety = (screen->ScrHeight/2);
  70.   double temp;
  71.   double angle=0;
  72.   ULONG  colour;
  73.   double Z2,X2,Y2;
  74.   ULONG  jport1;
  75.   LONG   scale=32;
  76.   UWORD  anglex=0,angley=0,anglez=0;
  77.   double u,v;
  78.   double *sine;       /* Pointer to our sine table */
  79.   double *cosine;     /* Pointer to our cosine table */
  80.  
  81.   object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_ANY);
  82.   sine   = AllocMemBlock(sizeof(double)*360,MEM_ANY);
  83.   cosine = AllocMemBlock(sizeof(double)*360,MEM_ANY);
  84.  
  85.   /* First calculate the X, Y and Z coordinates of our object. */
  86.  
  87.   for (i=0; i<AMTDOTS; i++) {
  88.     u = ((double)FastRandom(28000-1)+1)/10000-1.4;  /*  -1.4 < u < 1.4  */
  89.     v = ((double)FastRandom(28000-1)+1)/10000-1.4;  /*  -1.4 < v < 1.4  */
  90.     object[i].X  = u*(1+v*v)-u*u*u;
  91.     object[i].Y  = v*(1+u*u)-v*v*v;
  92.     object[i].Z  = u*u-v*v;
  93.   }
  94.  
  95.   /* Now generate our cosine and sinus tables */
  96.  
  97.   for (i=0; i<360; i++) {
  98.     cosine[i] = cos(angle);
  99.     sine[i]   = sin(angle);
  100.     angle    += 0.25;
  101.   }
  102.  
  103.   /* Go into our main loop */
  104.  
  105.   InitJoyPorts();
  106.  
  107.   do
  108.   {
  109.     jport1 = ReadJoyPort(JPORT1,JT_ZBXY);
  110.     scale += GetY(jport1);
  111.     if (scale < 1) scale = 1;
  112.     if (scale > 100) scale = 100;
  113.  
  114.     ClearBitmap(screen->Bitmap);
  115.  
  116.     for (i=0; i<AMTDOTS; i++) {
  117.  
  118.       X2 = object[i].X;
  119.       Y2 = object[i].Y;
  120.       Z2 = object[i].Z;
  121.  
  122.       /* Rotate the X axis */
  123.  
  124.       temp = Z2;
  125.       Z2 = Z2*cosine[anglex] - Y2*sine[anglex];
  126.       Y2 = Y2*cosine[anglex] + temp*sine[anglex];
  127.  
  128.       /* Rotate the Y axis */
  129.  
  130.       temp = Z2;
  131.       Z2 = Z2*cosine[angley] - X2*sine[angley];
  132.       X2 = X2*cosine[angley] + temp*sine[angley];
  133.  
  134.       /* Rotate the Z axis */
  135.  
  136. //      temp = X2;
  137. //      X2 = X2*cosine[anglez] - Y2*sine[anglez];
  138. //      Y2 = Y2*cosine[anglez] + temp*sine[anglez];
  139.  
  140.       /* Calculate colour based on Z position (-1.96 < Z < +1.96) */
  141.  
  142.       colour = (((Z2+MAXZ)/MAXZ)*screen->AmtColours)/2;
  143.  
  144.       /* Finally scale the (x,y) coordinates to enlarge or shrink the sphere */
  145.  
  146.       X2 *= scale;
  147.       Y2 *= scale;
  148.  
  149.       DrawPixel(screen->Bitmap,(WORD)X2+offsetx,(WORD)Y2+offsety,colour);
  150.     }
  151.  
  152.     anglex++; if (anglex >= 360) anglex = 0;
  153.     angley++; if (angley >= 360) angley = 0;
  154.     anglez++; if (anglez >= 360) anglez = 0;
  155.  
  156.     WaitVBL();
  157.     SwapBuffers(screen);
  158.   } while(!(jport1 & MB_LMB));
  159.  
  160.   FreeMemBlock(object);
  161.   FreeMemBlock(sine);
  162.   FreeMemBlock(cosine);
  163. }
  164.  
  165.